// pages/my/my.js
var config = require('../../config');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zanList: [{
      image_url: '/images/blank.jpg'
    }],
    connectList: [{
      image_url: '/images/blank.jpg'
    }],
    list: [{
      image_url: '/images/blank.jpg'
    }],
    userInfo: null,
    type: 'connect',
    zan_total_page: 0,
    connect_total_page: 0,
    zan_now_page: 1,
    connect_now_page: 1,
    loading_display: 'display:none'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    // 获取用户信息.
    app.getUserInfo(function (err, userInfo) {
      if (!err) {
        self.setData({
          userInfo: userInfo
        });
      }
    });

    // 获取初始数据.
    app.getUserOpenId(function (err, openid) {
      if (!err) {
        self.initMyData(openid);
      }
    });
  },
  // 获取初始化数据.
  initMyData: function (openid) {
    var self = this;

    // 先从缓存取值.
    if (self.data.type == 'connect') {
      wx.getStorage({
        key: 'my_connect_list',
        success: function (res) {
          self.setData({
            list: res.data
          })
        },
      })
    } else {
      wx.getStorage({
        key: 'my_zan_list',
        success: function (res) {
          self.setData({
            list: res.data
          })
        },
      })
    }
    wx.request({
      url: config.my_init_data_url,
      data: {
        openid: openid
      },
      success: function (res) {
        app.globalData.myChanged = false;
        if (res.statusCode == 200 && res.data) {
          self.data.zan_total_page = res.data.zan_total_page;
          self.data.connect_total_page = res.data.connect_total_page;
          self.data.zanList = res.data.zan_list;
          self.data.connectList = res.data.connect_list;
          // 更新缓存
          wx.setStorage({
            key: 'my_connect_list',
            data: res.data.connect_list,
          });
          wx.setStorage({
            key: 'my_zan_list',
            data: res.data.zan_list,
          })


          if (self.data.type == 'connect') {
            self.setData({
              list: res.data.connect_list
            });
          } else {
            self.setData({
              list: res.data.zan_list
            });
          }
        } else {
          wx.showToast({
            title: '网络请求失败'
          })
        }
      }
    })
  },
  // 切换到我的收藏
  myConnect: function () {
    var self = this;
    self.data.type = 'connect';
    self.setData({
      type: 'connect',
      list: self.data.connectList
    });
  },
  // 切换到我的点赞
  myZan: function () {
    var self = this;
    self.data.type = 'zan';
    self.setData({
      type: 'zan',
      list: self.data.zanList
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.myChanged) {
      var self = this;
      self.data.zan_now_page = 1;
      self.data.connect_now_page = 1;
      app.getUserOpenId(function (err, openid) {
        if (!err) {
          self.initMyData(openid);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var self = this;
    app.getUserOpenId(function (err, openid) {
      self.getPostList(openid);
    });
  },
  getPostList: function (openid) {
    var self = this;

    // loading
    self.setData({
      loading_display: 'display: block;'
    });

    var page = 0;
    var totalPage = 0;
    var list = '';
    if (self.data.type == 'connect') {
      page = parseInt(self.data.connect_now_page + 1);
      totalPage = self.data.connect_total_page;
      list = self.data.connectList;
    } else {
      page = parseInt(self.data.zan_now_page + 1);
      totalPage = self.data.zan_total_page;
      list = self.data.zanList;
    }
    if (page <= totalPage) {
      wx.request({
        url: config.my_post_url,
        data: {
          openid: openid,
          page: page,
          type: self.data.type
        },
        success: function (res) {
          if (res.statusCode == 200) {
            Array.prototype.push.apply(list, res.data);
            if (self.data.type == 'connect') {
              self.data.connectList = list;
              self.data.connect_now_page = page;
            } else {
              self.data.zanList = list;
              self.data.zan_now_page = page;
            }
            self.setData({
              list: list,
              loading_display: 'display:none;'
            });
          } else {
            self.setData({
              loading_display: 'display:none;'
            });
            wx.showToast({
              title: '网络请求失败，请重试',
            })
          }
        }
      })
    } else {
      self.setData({
        loading_display: 'display:none;',
        endMsg: '已经到底了，没有更多文章了',
      });
    }
  },
  onShareAppMessage: function () {
    return {
      title: '爱美文，精典美文与诗歌收集珍藏。',
      path: '/pages/tui/tui',
      imageUrl: '/images/xiaochengxu.jpg',
    };
  }
})